package com.itools.core.service.impl;

import com.itools.core.base.CommonResult;
import com.itools.core.bean.BeanCopyUtils;
import com.itools.core.context.FileStrategyServiceContext;
import com.itools.core.param.FmsUploadTokenParam;
import com.itools.core.result.*;
import com.itools.core.service.FileHandleService;
import com.itools.core.service.FmsFileHandleStrategyService;
import com.itools.core.service.FmsFileSystemService;
import com.itools.core.system.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @project: itools-backend
 * @description:
 * @author: XUCHANG
 * @create: 2021-04-05 14:44
 */
@Service
public class FmsFileHandleStrategyServiceImpl extends AbstractService implements FmsFileHandleStrategyService {
    @Autowired
    private FileStrategyServiceContext fileStrategyServiceContext;
    @Autowired
    private FileHandleService fileHandleService;
    @Autowired
    private FmsFileSystemService fmsFileSystemService;

    /**
     * 单个文件上传
     *
     * @param file        文件
     * @param uploadToken 上传文件token
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult<FmsFileUploadResult> singletonUploadFile(MultipartFile file, String uploadToken) {
        //压缩过后的文件名称前端无法获取，额外的字段标识
        String originalFilename = file.getOriginalFilename();
        //验证文件token
        CommonResult<FmsUploadTokenResult> fmsUploadTokenResultCommonResult = fileHandleService.getFileDetailByToken(uploadToken);

        FmsBusinessTypeResult businessType = fmsUploadTokenResultCommonResult.getData().getBusinessTypeResult();
        //验证文件
        fmsFileSystemService.verifyFiles(file,businessType);
        FmsUploadTokenParam uploadTokenParam = new FmsUploadTokenParam();
        BeanCopyUtils.copyProperties(fmsUploadTokenResultCommonResult.getData(),uploadTokenParam);
        //执行文件上传，根据配置的不同策略处理文件上传
        return fileStrategyServiceContext.get().singletonUploadFile(file, uploadToken, uploadTokenParam, originalFilename);
    }

    /**
     * 批量上传文件
     * @param files 文件
     * @param uploadToken 文件访问的token
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult<List<FmsMultipartFileResult>> filesMultipartUpload(MultipartFile[] files, String uploadToken) {
        //验证文件token
        CommonResult<FmsUploadTokenResult> fmsUploadTokenResultCommonResult = fileHandleService.getFileDetailByToken(uploadToken);

        FmsBusinessTypeResult businessType = fmsUploadTokenResultCommonResult.getData().getBusinessTypeResult();
        //验证文件
        fmsFileSystemService.verifyFiles(files,businessType);
        FmsUploadTokenParam uploadTokenParam = new FmsUploadTokenParam();
        BeanCopyUtils.copyProperties(fmsUploadTokenResultCommonResult.getData(),uploadTokenParam);
        //执行文件上传，根据配置的不同策略处理文件上传
        return fileStrategyServiceContext.get().mutipartUploadFiles(files, uploadToken, uploadTokenParam);
    }

    /**
     * 获取文件访问路径
     *
     * @param uniqueIds      List文件唯一id
     * @param expiredTime    过期时间
     * @param maxAccessCount 最大访问次数
     * @param type           文件下载 download/展示 show
     * @return
     */
    @Override
    public CommonResult<List<FmsFileRecordPathResult>> getFileUrlByUniqueIds(List<String> uniqueIds, Integer expiredTime, Integer maxAccessCount, String type) {
        return fileStrategyServiceContext.get().getFileUrlByUniqueIds(uniqueIds,expiredTime,maxAccessCount,type);
    }

    /**
     * 下载文件
     *
     * @param fileAccessToken 文件的访问token
     * @param response
     * @param uniqueId        文件唯一id
     */
    @Override
    public void downloadFile(String fileAccessToken, HttpServletResponse response, String uniqueId) {
        fileStrategyServiceContext.get().downloadFile(fileAccessToken,response,uniqueId);
    }
}
